package cn.jy.nvr.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sun.net.ssl.internal.ssl.Provider;
import com.sun.org.apache.bcel.internal.generic.NEW;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.ProviderProcess;
import cn.jy.nvr.mapper.ProviderAllotMapper;
import cn.jy.nvr.mapper.ProviderBodyCheckMapper;
import cn.jy.nvr.mapper.ProviderCollectionMapper;
import cn.jy.nvr.mapper.ProviderCostMapper;
import cn.jy.nvr.mapper.ProviderHeatMapper;
import cn.jy.nvr.mapper.ProviderProcessMapper;
import cn.jy.nvr.mapper.ProviderRegisterMapper;
import cn.jy.nvr.service.ProviderProcessQuartzJob;
import cn.jy.nvr.utils.TimeUtils;
@Service("ProviderProcessquartzJob")
public class ProviderProcessQuartzJobImpl implements ProviderProcessQuartzJob{
	@Autowired 
	ProviderRegisterMapper Registermapper;
	@Autowired
	ProviderBodyCheckMapper bodyCheckMapper;
	@Autowired 
	ProviderAllotMapper allotMapper;
	@Autowired 
	ProviderCollectionMapper collectionMapper;
	@Autowired
	ProviderHeatMapper heatMapper;
	@Autowired
	ProviderCostMapper costMapper;
	@Autowired
	ProviderProcessMapper  processMapper;
	Logger logger=Logger.getLogger(ProviderProcessQuartzJobImpl.class);
   public void work(){
	   logger.info("统计当前献浆状况");
	   //当前日期
	   String nowDate=new SimpleDateFormat("yyyy-MM-dd").format( new Date());
	  
	   ProviderProcess providerProcess=new ProviderProcess();
	   providerProcess.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format( new Date()));
	   providerProcess.setDate(nowDate);
	   //获取当前登记人数
	   DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
	   String providerRegisters = Registermapper.getProviderRegistersFromJingYing(nowDate);
	   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
	   if(providerRegisters.isEmpty()){
		  providerProcess.setRegister("-1"); 
	   }
	   logger.info("登记人数"+providerRegisters);
	   providerProcess.setRegister(providerRegisters);
	   
	   //获取当前分配人数
	   String providerAllots = allotMapper.getProviderAllots(nowDate);
	   if(providerAllots.isEmpty()){
		   providerProcess.setAllot("-1");
	   }
	   logger.info("分配人数"+providerAllots);
	   providerProcess.setAllot(providerAllots);
	   //获取当前采浆前人数
	   String beforeCollections = collectionMapper.getBeforeCollections(nowDate);
	   if(beforeCollections.isEmpty()){
		   providerProcess.setBeforeCollection("-1");
	   }
	   logger.info("采浆前人数"+beforeCollections);
	   providerProcess.setBeforeCollection(beforeCollections);
	   //获取当前采浆后的人数
	   String afterCollections = collectionMapper.getAfterCollections(nowDate);
	   if(afterCollections.isEmpty()){
		   providerProcess.setAfterCollection("-1");
	   }
	   logger.info("采浆后人数"+afterCollections);
	   providerProcess.setAfterCollection(afterCollections);
	  
	
	   DataSourceContextHolder.setDataSourceType(DataSourceConst.jingying);
	   //热和的血浆数
	   try {
		String providerHeats = heatMapper.getProviderHeats(nowDate);
		   if(providerHeats.isEmpty()){
			 providerProcess.setHeat("-1");  
		   }
		  logger.info("热和人数"+providerHeats);
		   providerProcess.setHeat(providerHeats);
	} catch (Exception e) {
		e.printStackTrace();
	}
	 //获取当前体检人数
	   String providerBodyChecks = bodyCheckMapper.getProviderBodyChecksFromJingYing(nowDate);
	   if(providerBodyChecks.isEmpty()){
		   providerProcess.setBodyCheck("-1");
	   }
	   logger.info("体检人数"+providerBodyChecks);
	   providerProcess.setBodyCheck(providerBodyChecks);
	   //费用发放人数
	   String providerCosts = costMapper.getProviderCostsFromJingYing(nowDate);
	   if(providerCosts.isEmpty()){
		   providerProcess.setCost("-1");
	   }
      logger.info("费用发放人数"+providerCosts);	   
	   providerProcess.setCost(providerCosts);
	   
	   DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
	   processMapper.save(providerProcess);  
	   logger.info("以保存到数据库");
   }
   
	
}
